<?php
/**
 * Created by PhpStorm.
 * User: liujun
 * Date: 2018/7/4
 * Time: 下午11:22
 */
use Dai\Framework\Library\Trace;
use Phalcon\Di\FactoryDefault;


error_reporting(E_ALL);

define('APP_PATH', dirname(__DIR__)."/../");
define('BASE_PATH', APP_PATH . '/');
define('CONFIG_PATH', APP_PATH . '/config/');
define('PRJ_NS', 'PddWechat');

require_once BASE_PATH . "/vendor/autoload.php";

/**
 * The FactoryDefault Dependency Injector automatically registers
 * the services that provide a full stack framework.
 */
$di = new FactoryDefault();

/**
 * Service init
 */
(new \Dai\Framework\Init\Service()) ->init($di);

$wxTemplate = new Wxtemplate();
$useData = new \PddWechat\Models\User\Data\UserData();
$users = $useData->getList(0, 1000);
foreach ($users as $user){
    /** @var \PddWechat\Models\User\DataObject\UserDo $user */
    $ret = $wxTemplate->sendCustomer($user->id, $user->openId, $user->userName);
    if( $ret == false && $user->subscribe === 1) {
        $wxTemplate->sendAppTemplate($user->id, $user->openId, $user->userName);
    }
}
echo "发送完毕\n";

class Wxtemplate
{
    public function sendCustomer($userId, $openId, $nickName){
        $articles = \PddWechat\Library\MsgLibrary::getCustomerMsg( $nickName);
        if( $articles === false) {
            return false;
        }
        $template = [
            'touser' => $openId,
            'msgtype' => "news",
            'news' => [
                'articles' => $articles,
            ],
        ];
        return \PddWechat\Library\WechatLibrary::sendCustomer($userId, $openId, json_encode($template, JSON_UNESCAPED_UNICODE ));
    }

    public function sendAppTemplate($userId, $openId, $nickName, $topColor = '#7B68EE')
    {
        $wordColor="#173177";
        $template = [
            'touser' => $openId,
            'template_id' => "p4I34omwwPC9Y4cV4n_p6gtqXrr1sAZ1l9Er1kAjO-Q",
            'url' => "https://mobile.yangkeduo.com/duo_roulette.html?pid=1884871_22329699&cpsSign=CL1884871_22329699_84873959b2324bc085f9a89376d8c668&duoduo_type=2",
            'topcolor' => $topColor,
        ];

        $template['data'] = [
            'first' => ['value' => "@$nickName,转盘抽大奖，幸运迎免单 ", 'color' => $wordColor],
            'keyword1' => ['value' => "抽奖", 'color' => $wordColor],
            'keyword2' => ['value' => "大转盘抽奖，最高100元现金红包或者直接免单", 'color' => $wordColor],
            'keyword3' => ['value' => "赶紧来试试手气吧", 'color' => $wordColor],
        ];

        \PddWechat\Library\WechatLibrary::sendTemplate($userId, $openId, json_encode($template, JSON_UNESCAPED_UNICODE ));
    }
}
